//
//  ZJWatchCell.swift
//  mini
//
//  Created by Mac on 2017/5/19.
//  Copyright © 2017年 Mac. All rights reserved.
//

import UIKit
import SnapKit
class ZJWatchCell: UITableViewCell {
    
    
    var title:String?{
        
        didSet{
            self.titleLabel?.text = title
        }
    }
    var time :String?{
        didSet{
            self.timeLabel?.text = time
        }
    }
    var content:String?{
        didSet{
            self.contentLabel?.text = content
        }
    }
    var contentV:UIView?
    var titleLabel:UILabel?
    var contentLabel:UILabel?
    var timeLabel:UILabel?
    

    class func typeViewWithTableView(tableView:UITableView) -> ZJWatchCell? {
        
        let ID:String = "RFTypeViewCell";
        
        //如果使用as！就回以为cell一定存在 if (cell == nil) 就不会成立，但是cell！会出现 fatal error: unexpectedly found nil while unwrapping an Optional value 
//        总结使用as？
        var  cell:ZJWatchCell? = tableView.dequeueReusableCell(withIdentifier: ID) as?
        ZJWatchCell
        if (cell == nil) {
            cell = ZJWatchCell(style: .default, reuseIdentifier: ID)
            cell?.selectionStyle = .none;
            
        }
        return cell!;
        
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupuI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupuI()  {
        
        self.backgroundColor = .white
        
        contentV = UIView()
        
        if #available(iOS 10.0, *) {
            contentV?.backgroundColor = UIColor(displayP3Red: 242.0 / 256.0, green: 242.0 / 256.0, blue: 242.0 / 256.0, alpha: 1.0)
        } else {
            
            contentV?.backgroundColor = UIColor(colorLiteralRed:  242.0 / 256.0, green: 242.0 / 256.0, blue: 242.0 / 256.0, alpha: 1.0)
            
        }
        
        contentV?.layer.cornerRadius = 10
        
        contentV?.layer.masksToBounds = true
        
        contentView.addSubview(contentV!)
        
        titleLabel = UILabel()
        
        titleLabel?.textColor = .black
        
        titleLabel?.font = UIFont.systemFont(ofSize: 22)
        
        titleLabel?.textAlignment = .left
        
        contentV?.addSubview(titleLabel!)
        
        contentLabel = UILabel()
        
        contentLabel?.textColor = kWatchColor
        
        contentLabel?.font = UIFont.systemFont(ofSize: 30)
        
        contentLabel?.textAlignment = .right
        
        contentV?.addSubview(contentLabel!)
        
        timeLabel = UILabel()
        
        timeLabel?.textColor = .gray
        
        timeLabel?.font = UIFont.systemFont(ofSize: 13)
        
        timeLabel?.textAlignment = .right
        
        contentV?.addSubview(timeLabel!)
        
        setupContrast()
        
    }
    
    
    func setupContrast() {
        
        contentV?.snp.makeConstraints { (make) in
            
            make.width.equalTo(ScreenWidth-2*10)
            
            make.top.equalTo(self.contentView).offset(10)
            
            make.left.equalTo(self.contentView).offset(10)
        
            make.bottom.equalTo(self.contentView)
            
        }
        
        titleLabel?.snp.makeConstraints({ (make) in
            
            make.top.equalTo(contentV!).offset(10*2)
            
            make.left.equalTo(contentV!).offset(10*2)
        })
        
        contentLabel?.snp.makeConstraints({ (make) in
            
            make.top.equalTo(contentV!).offset(10)
            
            make.right.equalTo(contentV!).offset(-10*2)
        })
        timeLabel?.snp.makeConstraints({ (make) in
            
            make.top.equalTo((contentLabel?.snp.bottom)!).offset(10*2)
            
            make.bottom.equalTo(contentV!).offset(-10)
            
            make.right.equalTo(contentV!).offset(-10*2);
        })
        
        contentView.snp.makeConstraints { (make) in
        
              make.top.leading.trailing.bottom.equalTo(self)
        }
        
    }
    
}
